home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_4.9 / globalfeats / globalfeats.c next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  4.6 KB  |  162 lines

  1. /* 
  2.  * globalfeats.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /* GLOBALFEATS: program determines global features of image
  10.  *                    usage: globalfeats inimg [-L]
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include "tiffimage.h"          /* picfile info on images */
  19. #include "images.h"
  20. extern void print_sos_lic ();
  21.  
  22. #define OFF 0
  23. #define ON 255
  24.  
  25. long usage (short);
  26. long input (int, char **);
  27.  
  28. main (argc, argv)
  29.      int argc;
  30.      char *argv[];
  31. {
  32.   Image *imgI;                  /* I/O image structure */
  33.   unsigned char **image;        /* input/output image */
  34.   long height, width;           /* size of I/O images */
  35.   long nPix;                    /* no. pixels total (minus 1-pix borders) */
  36.   long nOn;                     /* no. of ON-valued pixels */
  37.   double sumXOn, sumYOn;        /* sum of x,y of ON-pixels */
  38.   double sumXOnSq, sumYOnSq;    /* sum of sqrs. of x,y of ON-pixels */
  39.   long sumEdge;                 /* number of edge pixels */
  40.   double avgX, avgY;            /* average x,y locations */
  41.   double stdDevX, stdDevY;      /* std. deviation of x,y locations */
  42.  
  43.   long value;
  44.   long x, y;
  45.  
  46. /* user input */
  47.   if (input (argc, argv) < 0)
  48.     return (-1);
  49.  
  50.   imgI = ImageIn (argv[1]);
  51.   image = imgI->img;
  52.   height = ImageGetHeight (imgI);
  53.   width = ImageGetWidth (imgI);
  54.  
  55. /* initialize */
  56.   nPix = nOn = 0;
  57.   sumXOn = sumYOn = 0;
  58.   sumXOnSq = sumYOnSq = 0;
  59.   sumEdge = 0;
  60.  
  61. /* determine global features */
  62.   for (y = 1; y < height - 1; y++) {
  63.     for (x = 1; x < width - 1; x++) {
  64.       value = (long) image[y][x];
  65.       nPix++;
  66.       if (value != OFF) {
  67.         nOn++;
  68.         sumXOn += x;
  69.         sumYOn += y;
  70.         sumXOnSq += x * x;
  71.         sumYOnSq += y * y;
  72.         if (image[y - 1][x] == OFF || image[y - 1][x + 1] == OFF
  73.             || image[y][x + 1] == OFF || image[y + 1][x + 1] == OFF
  74.             || image[y + 1][x] == OFF || image[y + 1][x - 1] == OFF
  75.             || image[y][x - 1] == OFF || image[y - 1][x - 1] == OFF)
  76.           sumEdge++;
  77.       }
  78.     }
  79.   }
  80.  
  81.   avgX = (double) sumXOn / (double) nOn;
  82.   avgY = (double) sumYOn / (double) nOn;
  83.   stdDevX = sqrt (sumXOnSq / nOn - avgX * avgX);
  84.   stdDevY = sqrt (sumYOnSq / nOn - avgY * avgY);
  85.  
  86. /* list results of global feature determination */
  87.   printf ("Image size: %dx%d\n", width, height);
  88.   printf ("Image size within 1-pixel border: %dx%d\n",
  89.           width - 2, height - 2);
  90.   printf ("Number of Pixels: %d\n", nPix);
  91.   printf ("Number of ON-pixels: %d, (%5.1f%%)\n", nOn,
  92.           (double) nOn / (double) nPix * 100.0);
  93.   printf ("1st Moment: (%5.2f, %5.2f)\n", avgX, avgY);
  94.   printf ("2nd Moment: (%5.2f, %5.2f)\n", stdDevX, stdDevY);
  95.   printf ("Number of Edge Pixels: %d, (%5.1f%%)\n",
  96.           sumEdge, (double) sumEdge / (double) nPix * 100.0);
  97.  
  98.   return (0);
  99. }
  100.  
  101.  
  102.  
  103. /* USAGE:       function gives instructions on usage of program
  104.  *                    usage: usage (flag)
  105.  *              When flag is 1, the long message is given, 0 gives short.
  106.  */
  107.  
  108. long
  109. usage (flag)
  110.      short flag;                /* flag =1 for long message; =0 for short message */
  111. {
  112.  
  113. /* print short usage message or long */
  114.   printf ("USAGE: globalfeats inimg [-L]\n");
  115.   if (flag == 0)
  116.     return (-1);
  117.  
  118.   printf ("\nglobalfeats determines some global features\n");
  119.   printf ("of input image and prints out these results to stdout;\n");
  120.   printf ("global features determined are:\n");
  121.   printf ("  -- image size\n");
  122.   printf ("  -- total number of pixels\n");
  123.   printf ("  -- number of ON-valued pixels (where ON is black intensity)\n");
  124.   printf ("  -- percentage of ON-valued pixels to total pixels\n");
  125.   printf ("  -- 1st moment or average x,y coordinate location of ON-pixels\n");
  126.   printf ("  -- 2nd moment of x,y locations of ON-pixels\n");
  127.   printf ("  --  number of edge pixels\n");
  128.   printf ("  -- percentage of edge pixels to total pixels.\n");
  129.   printf ("  NOTE: that the calculations are performed on all pixels\n");
  130.   printf ("  EXCEPT the 1-pixel wide borders of pixels at the sides\n");
  131.   printf ("  of the image.\n\n");
  132.   printf ("ARGUMENTS:\n");
  133.   printf ("    inimg: input image filename (TIF)\n\n");
  134.   printf ("OPTIONS:\n");
  135.   printf ("       -L: print Software License for this module\n");
  136.  
  137.   return (-1);
  138. }
  139.  
  140.  
  141. /* INPUT:       function reads input parameters
  142.  *                  usage: input (argc, argv)
  143.  */
  144.  
  145. #define USAGE_EXIT(VALUE) {usage (VALUE); return (-1);}
  146.  
  147. long
  148. input (argc, argv)
  149.      int argc;
  150.      char *argv[];
  151. {
  152.  
  153.   if (argc == 1)
  154.     USAGE_EXIT (1);
  155.   if (argc >= 3 && strcmp (argv[2], "-L") == 0) {
  156.     print_sos_lic ();
  157.     exit (0);
  158.   }
  159.  
  160.   return (0);
  161. }
  162.